home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 22 xml and adonet / xmldemo / domform.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  14.0 KB  |  362 lines

  1. Imports System.Xml
  2. Imports System.Xml.Xsl
  3.  
  4. Public Class DomForm
  5.     Inherits System.Windows.Forms.Form
  6.  
  7. #Region " Windows Form Designer generated code "
  8.  
  9.     Public Sub New()
  10.         MyBase.New()
  11.  
  12.         'This call is required by the Windows Form Designer.
  13.         InitializeComponent()
  14.  
  15.         'Add any initialization after the InitializeComponent() call
  16.  
  17.     End Sub
  18.  
  19.     'Form overrides dispose to clean up the component list.
  20.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  21.         If disposing Then
  22.             If Not (components Is Nothing) Then
  23.                 components.Dispose()
  24.             End If
  25.         End If
  26.         MyBase.Dispose(disposing)
  27.     End Sub
  28.  
  29.     'Required by the Windows Form Designer
  30.     Private components As System.ComponentModel.IContainer
  31.  
  32.     'NOTE: The following procedure is required by the Windows Form Designer
  33.     'It can be modified using the Windows Form Designer.  
  34.     'Do not modify it using the code editor.
  35.     Friend WithEvents btnLoad As System.Windows.Forms.Button
  36.     Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
  37.     Friend WithEvents btnRemove As System.Windows.Forms.Button
  38.     Friend WithEvents btnCreate As System.Windows.Forms.Button
  39.     Friend WithEvents btnSearch As System.Windows.Forms.Button
  40.     Friend WithEvents txtXPath As System.Windows.Forms.TextBox
  41.     Friend WithEvents btnTransform As System.Windows.Forms.Button
  42.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  43.         Me.btnLoad = New System.Windows.Forms.Button()
  44.         Me.TreeView1 = New System.Windows.Forms.TreeView()
  45.         Me.btnRemove = New System.Windows.Forms.Button()
  46.         Me.btnCreate = New System.Windows.Forms.Button()
  47.         Me.btnSearch = New System.Windows.Forms.Button()
  48.         Me.txtXPath = New System.Windows.Forms.TextBox()
  49.         Me.btnTransform = New System.Windows.Forms.Button()
  50.         Me.SuspendLayout()
  51.         '
  52.         'btnLoad
  53.         '
  54.         Me.btnLoad.Location = New System.Drawing.Point(8, 16)
  55.         Me.btnLoad.Name = "btnLoad"
  56.         Me.btnLoad.Size = New System.Drawing.Size(96, 40)
  57.         Me.btnLoad.TabIndex = 0
  58.         Me.btnLoad.Text = "Load and display"
  59.         '
  60.         'TreeView1
  61.         '
  62.         Me.TreeView1.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
  63.                     Or System.Windows.Forms.AnchorStyles.Left) _
  64.                     Or System.Windows.Forms.AnchorStyles.Right)
  65.         Me.TreeView1.ImageIndex = -1
  66.         Me.TreeView1.Location = New System.Drawing.Point(120, 16)
  67.         Me.TreeView1.Name = "TreeView1"
  68.         Me.TreeView1.SelectedImageIndex = -1
  69.         Me.TreeView1.Size = New System.Drawing.Size(384, 296)
  70.         Me.TreeView1.TabIndex = 1
  71.         '
  72.         'btnRemove
  73.         '
  74.         Me.btnRemove.Location = New System.Drawing.Point(8, 72)
  75.         Me.btnRemove.Name = "btnRemove"
  76.         Me.btnRemove.Size = New System.Drawing.Size(96, 40)
  77.         Me.btnRemove.TabIndex = 2
  78.         Me.btnRemove.Text = "Remove Nodes "
  79.         '
  80.         'btnCreate
  81.         '
  82.         Me.btnCreate.Location = New System.Drawing.Point(8, 128)
  83.         Me.btnCreate.Name = "btnCreate"
  84.         Me.btnCreate.Size = New System.Drawing.Size(96, 40)
  85.         Me.btnCreate.TabIndex = 3
  86.         Me.btnCreate.Text = "Create Nodes"
  87.         '
  88.         'btnSearch
  89.         '
  90.         Me.btnSearch.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left)
  91.         Me.btnSearch.Location = New System.Drawing.Point(8, 312)
  92.         Me.btnSearch.Name = "btnSearch"
  93.         Me.btnSearch.Size = New System.Drawing.Size(96, 40)
  94.         Me.btnSearch.TabIndex = 4
  95.         Me.btnSearch.Text = "Search Nodes"
  96.         '
  97.         'txtXPath
  98.         '
  99.         Me.txtXPath.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
  100.                     Or System.Windows.Forms.AnchorStyles.Right)
  101.         Me.txtXPath.Location = New System.Drawing.Point(120, 320)
  102.         Me.txtXPath.Name = "txtXPath"
  103.         Me.txtXPath.Size = New System.Drawing.Size(384, 24)
  104.         Me.txtXPath.TabIndex = 5
  105.         Me.txtXPath.Text = ""
  106.         '
  107.         'btnTransform
  108.         '
  109.         Me.btnTransform.Location = New System.Drawing.Point(8, 184)
  110.         Me.btnTransform.Name = "btnTransform"
  111.         Me.btnTransform.Size = New System.Drawing.Size(96, 40)
  112.         Me.btnTransform.TabIndex = 6
  113.         Me.btnTransform.Text = "Transform wtih XSLT"
  114.         '
  115.         'DomForm
  116.         '
  117.         Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
  118.         Me.ClientSize = New System.Drawing.Size(520, 357)
  119.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnTransform, Me.txtXPath, Me.btnSearch, Me.btnCreate, Me.btnRemove, Me.TreeView1, Me.btnLoad})
  120.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  121.         Me.Name = "DomForm"
  122.         Me.Text = "XmlDocument class"
  123.         Me.ResumeLayout(False)
  124.  
  125.     End Sub
  126.  
  127. #End Region
  128.  
  129.     ' load the XML file
  130.  
  131.     Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
  132.         DisplayXmlFile("employees.xml", TreeView1)
  133.     End Sub
  134.  
  135.     ' remove some elements
  136.  
  137.     Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
  138.         ' Load the XML document.
  139.         Dim xmldoc As New XmlDocument()
  140.         xmldoc.Load("employees.xml")
  141.  
  142.         ' Remove the ID attribute and the birthDate sub-element from all
  143.         ' Employee elements.
  144.         Dim xmlEl As XmlElement
  145.         For Each xmlEl In xmldoc.DocumentElement.ChildNodes
  146.             ' remove the attribute with a given name.
  147.             xmlEl.RemoveAttribute("id")
  148.             ' Get a reference to the birthDate sub-element.
  149.             Dim xmlList As XmlNodeList = xmlEl.GetElementsByTagName("birthDate")
  150.             If xmlList.Count > 0 Then
  151.                 xmlEl.RemoveChild(xmlList(0))
  152.             End If
  153.         Next
  154.  
  155.         ' Add it to the TreeView Nodes collection
  156.         DisplayXmlNode(xmldoc, TreeView1.Nodes)
  157.     End Sub
  158.  
  159.  
  160.  
  161.     Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
  162.         ' Load the XML document.
  163.         Dim xmldoc As New XmlDocument()
  164.         xmldoc.Load("employees.xml")
  165.  
  166. #Const USE_CREATEAPPEND_ROUTINE = False
  167.  
  168. #If USE_CREATEAPPEND_ROUTINE Then
  169.         ' Create a new Employee element
  170.         Dim xmlEl As XmlElement = xmldoc.CreateElement("Employee")
  171.         ' Append it to the children collection of the DOM root element.
  172.         xmldoc.DocumentElement.AppendChild(xmlEl)
  173.  
  174.         ' Set the ID attribute of the new element.
  175.         xmlEl.SetAttribute("id", "100")
  176.         ' Create all its sub-elements.
  177.         Dim xmlChildEl As XmlElement
  178.  
  179.         ' This is the firstName sub-element.
  180.         xmlChildEl = xmldoc.CreateElement("firstName")
  181.         ' Create a child XmlText element for the firstName element.
  182.         Dim xmlText As XmlText = xmldoc.CreateTextNode("Joe")
  183.         xmlChildEl.AppendChild(xmlText)
  184.         ' Append the firstName element to the new Employee element.
  185.         xmlEl.AppendChild(xmlChildEl)
  186.  
  187.         ' This is the lastName sub-element.
  188.         xmlChildEl = xmldoc.CreateElement("lastName")
  189.         ' Create a child XmlText element and append it in one operation.
  190.         xmlChildEl.AppendChild(xmldoc.CreateTextNode("Doe"))
  191.         ' Append the lastName sub-Element to the new Employee element.
  192.         xmlEl.AppendChild(xmlChildEl)
  193.  
  194.         ' This is the address sub-element
  195.         xmlChildEl = xmldoc.CreateElement("address")
  196.         ' This time we use the InnerText property (a Microsoft extension to WC3 DOM).
  197.         xmlChildEl.InnerText = "1234 North Street"
  198.         ' Append the address sub-element to the new Employee element.
  199.         xmlEl.AppendChild(xmlChildEl)
  200.  
  201.         ' This is the city sub-element.
  202.         xmlChildEl = xmldoc.CreateElement("city")
  203.         xmlChildEl.InnerText = "Boston"
  204.         xmlEl.AppendChild(xmlChildEl)
  205. #Else
  206.         ' This version uses the CreateAppend auxiliary routine.
  207.  
  208.         ' Create a new Employee element.
  209.         Dim xmlEl As XmlElement = CreateAppendElement(xmldoc.DocumentElement, "Employee")
  210.         ' Set the ID attribute of the new element.
  211.         xmlEl.SetAttribute("id", "100")
  212.         ' Create all its sub-elements.
  213.         CreateAppendElement(xmlEl, "firstName", "Joe")
  214.         CreateAppendElement(xmlEl, "lastName", "Doe")
  215.         CreateAppendElement(xmlEl, "address", "1234 North Street")
  216.         CreateAppendElement(xmlEl, "city", "Boston")
  217. #End If
  218.  
  219.         ' Add it to the TreeView Nodes collection
  220.         DisplayXmlNode(xmldoc, TreeView1.Nodes)
  221.  
  222.         ' Save to a different file
  223.         xmldoc.Save("employees2.xml")
  224.     End Sub
  225.  
  226.     ' make an XPath search
  227.  
  228.     Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
  229.         ' Load the XML document.
  230.         Dim xmldoc As New XmlDocument()
  231.         xmldoc.Load("employees.xml")
  232.  
  233.         Dim xpath As String = txtXPath.Text
  234.         Dim xnl As XmlNodeList
  235.         Try
  236.             xnl = xmldoc.SelectNodes(xpath)
  237.         Catch ex As Exception
  238.             MessageBox.Show(ex.Message, "Invalid XPath syntax", MessageBoxButtons.OK, MessageBoxIcon.Error)
  239.             Exit Sub
  240.         End Try
  241.         Dim xn As XmlNode
  242.         Dim msg As String = ""
  243.  
  244.         For Each xn In xnl
  245.             msg &= xn.Name
  246.             If Not (xn.Value Is Nothing) Then msg &= "(" & xn.Value.ToString & ")"
  247.             msg &= ControlChars.CrLf
  248.  
  249.             Dim xmlEl As XmlElement = CType(xn, XmlElement)
  250.             Debug.WriteLine(xmlEl.GetElementsByTagName("lastName")(0).InnerText())
  251.         Next
  252.         MessageBox.Show(msg, "Found " & xnl.Count.ToString & " matches")
  253.  
  254.     End Sub
  255.  
  256.     ' make an XSL transform
  257.  
  258.     Private Sub btnTransform_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTransform.Click
  259.         ' Load the XSLT into an XmlTransform.
  260.         Dim xslTran As New XslTransform()
  261.         ' Load the .xslt file into it.
  262.         xslTran.Load("Employees.xslt")
  263.  
  264.         ' required action   
  265.         ' 0=convert to an HTML file
  266.         ' 1= use an XmlTextWriter
  267.         ' 2= create an XmlReader object
  268.  
  269. #Const ACTION = 2
  270.  
  271. #If ACTION = 0 Then
  272.         ' Convert the XML file into another XML file.
  273.         xslTran.Transform("employees.xml", "employees.html")
  274. #ElseIf ACTION = 1 Then
  275.         ' Load the XML document.
  276.         Dim xmldoc As New XmlDocument()
  277.         xmldoc.Load("employees.xml")
  278.  
  279.         Dim xtw As New XmlTextWriter("employees.html", System.Text.Encoding.UTF8)
  280.         xslTran.Transform(xmldoc.CreateNavigator, Nothing, xtw)
  281.         xtw.Close()
  282. #Else
  283.         ' Load the XML document.
  284.         Dim xmldoc As New XmlDocument()
  285.         xmldoc.Load("employees.xml")
  286.  
  287.         Dim xr As XmlReader = xslTran.Transform(xmldoc.CreateNavigator, Nothing)
  288.         Dim xmldoc2 As New XmlDocument()
  289.         xmldoc2.Load(xr)
  290.         xr.Close()
  291.         DisplayXmlNode(xmldoc2, TreeView1.Nodes)
  292. #End If
  293.  
  294.     End Sub
  295. End Class
  296.  
  297. ' common routines
  298.  
  299. Module XmlTreeViewSubs
  300.     ' display an XML file in a TreeView
  301.  
  302.     Sub DisplayXmlFile(ByVal filename As String, ByVal tvw As TreeView)
  303.         Dim xmldoc As New XmlDocument()
  304.         xmldoc.Load(filename)
  305.         ' Add it to the TreeView Nodes collection
  306.         DisplayXmlNode(xmldoc, tvw.Nodes)
  307.         ' Expand the root node.
  308.         tvw.Nodes(0).Expand()
  309.     End Sub
  310.  
  311.     Sub DisplayXmlNode(ByVal xmlnode As XmlNode, ByVal nodes As TreeNodeCollection)
  312.         ' Add a TreeView node for this XmlNode.
  313.         ' (Using the node's Name is OK for most XmlNode types.)
  314.         Dim tvNode As TreeNode = nodes.Add(xmlnode.Name)
  315.  
  316.         Select Case xmlnode.NodeType
  317.             Case XmlNodeType.Element
  318.                 ' This is an element: Check whether there are attributes.
  319.                 If xmlnode.Attributes.Count > 0 Then
  320.                     ' Create an ATTRIBUTES node.
  321.                     Dim attrNode As TreeNode = tvNode.Nodes.Add("(ATTRIBUTES)")
  322.                     ' Add all the attributes as children of the new node.
  323.                     Dim xmlAttr As XmlAttribute
  324.                     For Each xmlAttr In xmlnode.Attributes
  325.                         ' Each node shows name and value.
  326.                         attrNode.Nodes.Add(xmlAttr.Name & " = '" & xmlAttr.Value & "'")
  327.                     Next
  328.                 End If
  329.             Case XmlNodeType.Text, XmlNodeType.CDATA
  330.                 ' For these node types we display the value
  331.                 tvNode.Text = xmlnode.Value
  332.             Case XmlNodeType.Comment
  333.                 tvNode.Text = "<!--" & xmlnode.Value & "-->"
  334.             Case XmlNodeType.ProcessingInstruction, XmlNodeType.XmlDeclaration
  335.                 tvNode.Text = "<?" & xmlnode.Name & " " & xmlnode.Value & "?>"
  336.             Case Else
  337.                 ' ignore other node types.
  338.         End Select
  339.  
  340.         ' Call this routine recursively for each child node.
  341.         Dim xmlChild As XmlNode = xmlnode.FirstChild
  342.         Do Until xmlChild Is Nothing
  343.             DisplayXmlNode(xmlChild, tvNode.Nodes)
  344.             xmlChild = xmlChild.NextSibling
  345.         Loop
  346.     End Sub
  347.  
  348.     '  Create an XmlElement object with inner text and make it
  349.     '  a child of another XmlNode.
  350.  
  351.     Function CreateAppendElement(ByVal parentNode As XmlNode, ByVal name As String, Optional ByVal innerText As String = Nothing) As XmlElement
  352.         ' Create a new XmlElement object, set the return value.
  353.         Dim xmlEl As XmlElement = parentNode.OwnerDocument.CreateElement(name)
  354.         ' Set its inner text
  355.         If Not (innerText Is Nothing) Then xmlEl.InnerText = innerText
  356.         ' make it a child of its parent node.
  357.         parentNode.AppendChild(xmlEl)
  358.         ' Return the new node to the caller
  359.         Return xmlEl
  360.     End Function
  361.  
  362. End Module